﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Dev.Control;
using SfcorpDLL.biz;
using SfcorpDLL.ety;
using TT.Common;
using SQLHelper;
using DevExpress.XtraBars;
using TT.ORM;

namespace Sfcorp.form
{
    public partial class frmCntrCorp : BaseFormDev
    {
        bizCntrCorp bzCntrCorp;
        public frmCntrCorp()
        {
            InitializeComponent();
        }

        private string formSta;
        public string FormSta
        {
            set
            {
                formSta = value;
                switch (formSta)
                {
                    case "A":
                        lblBar.Caption = "新建状态";
                        break;
                    case "E":
                        lblBar.Caption = "编辑状态";
                        break;
                    default:
                        break;
                }
            }
            get
            {
                return formSta;
            }
        }
        #region 事件

        //显示行号
        private void gridView1_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            if (e.Info.IsRowIndicator)
            {
                if (e.RowHandle >= 0)
                {
                    e.Info.DisplayText = (e.RowHandle + 1).ToString();
                }
                else if (e.RowHandle < 0 && e.RowHandle > -1000)
                {
                    e.Info.Appearance.BackColor = System.Drawing.Color.AntiqueWhite;
                    e.Info.DisplayText = "G" + e.RowHandle.ToString();
                }
            }
        }
        private void frmCntrCorp_Load(object sender, EventArgs e)
        {
            bzCntrCorp = new bizCntrCorp(global.basSysPara);

            #region 下拉数据
            cboEnable_Flag.DataSource = dataProcessor.CopyIList<TT.ORM.etyCodeRef>(global.ilCustomerEnabled);
            #endregion

            #region 表格数据
            ggcClient.GridView = this.gridView1;
            ggcClient.GridView.OptionsBehavior.EditorShowMode = DevExpress.Utils.EditorShowMode.Click;
            string sMapingCols = "CNTR_CORP_COD,CNTR_CORP_NAM,ENABLE_FLAG,SEQ_NO,REMARKS";
            string sMapingHeaderTxt = "货代代码,货代名称,生效状态,排列顺序,备注";
            ggcClient.ColumnsMappingCreate(sMapingCols, sMapingHeaderTxt);
            this.winUtil.LoadGridLayout(ggcClient);

            RefreshDate();
            #endregion

            FormSta = "A";
            bar3.Text = "状态栏";

        }

        public override void InitForm()
        {
            #region 权限处理
            etyUsersModCtrl ey = new etyUsersModCtrl();
            ey.USER_ID = global.basSysPara.UserID;
            ey.MOD_ID = this.MOD_ID;
            global.bzUser.SetFormSecrity(this, barManager1.Items, ey);

            //btnRefresh.ItemClick+=new ItemClickEventHandler(btnRefresh_ItemClick);
            //btnSave.ItemClick+=new ItemClickEventHandler(btnSave_ItemClick);
            //btnNew.ItemClick+=new ItemClickEventHandler(btnNew_ItemClick);
            //btnDelete.ItemClick+=new ItemClickEventHandler(btnDelete_ItemClick);
            btnSec.ItemClick += new ItemClickEventHandler(btnSec_ItemClick);
            #endregion 

            base.InitForm();
        }

        private void btnRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            RefreshDate();
        }

        private void btnNew_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            NewData();
        }

        private void btnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SaveDate();
        }

        private void btnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DeleteDate();
        }

        private void btnSearch_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.gridView1.OptionsFind.AlwaysVisible = !this.gridView1.OptionsFind.AlwaysVisible;
        }

        private void btnExit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            F12();
        }
        private void ggcCntrCorp_Click(object sender, EventArgs e)
        {
            etyCntrCorp ey = srcBinding.Current as etyCntrCorp;
            if (ey == null)
                return;
            TT.Common.formProcess.SetFormEntity(ey, grpDtl);
            cboEnable_Flag.GetCodeText();
            FormSta = "E";
        }
        #endregion

        public override Dev.Control.etyQuickKey RefreshDate()
        {
            srcBinding.DataSource = bzCntrCorp.GetCntrCorp(new etyCntrCorp());
            ggcClient.DataSource = srcBinding;
            ggcClient.AutoColumnWidth();
            return base.RefreshDate();
        }

        public override etyQuickKey NewData()
        {
            FormSta = "A";
            etyCntrCorp ey = srcBinding.Current as etyCntrCorp;
            if (ey != null)
            {
                TT.Common.formProcess.SetFormEntity(ey, grpDtl);

                ClearForm();
            }
            return base.NewData();
        }

        public override etyQuickKey DeleteDate()
        {
            etyCntrCorp ey = srcBinding.Current as etyCntrCorp;
            if (ey == null)
                return null;
            if (TT.HelpInfo.MsgBox.ShowQuestionBox(string.Format("是否删除货代：{0}？", new string[] { ey.CNTR_CORP_NAM })))
            {
                if (bzCntrCorp.Delete_CntrCorp(ey).Exec_Success)
                {
                    TT.HelpInfo.MsgBox.ShowSuccessBox("删除成功");
                    lblBar.Caption = "";
                    RefreshDate();
                }
            }
            return base.DeleteDate();
        }

        public override etyQuickKey SaveDate()
        {
            etyCntrCorp ey = new etyCntrCorp();
            TT.Common.formProcess.GetFormEntity(ey, grpDtl);
            if (check_data(ey))
            {
                daoResult rst;
                if (FormSta == "A")
                    rst = bzCntrCorp.Insert_CntrCorp(ey);
                else
                    rst = bzCntrCorp.Update_CntrCorp(ey);
                if (rst.Exec_Success)
                {
                    TT.HelpInfo.MsgBox.ShowSuccessBox("保存成功");
                    RefreshDate();
                }
                else
                {
                    TT.HelpInfo.MsgBox.ShowWarnBox(rst.Error_String);
                    formProcess.SetControlFocus(rst.Focus_Control, grpDtl);
                }
            }
            return base.SaveDate();
        }

        #region 函数
        private void ClearForm()
        {
            txtCorpCod.Text = "";
            txtCorpNam.Text = "";
            cboEnable_Flag.SelectedIndex = -1;
            txtSeq_No.Text = "";
            txtRemark.Text = "";
        }
        private bool check_data(etyCntrCorp ey)
        {
            if (string.IsNullOrEmpty(ey.CNTR_CORP_COD.Trim()))
            {
                TT.HelpInfo.MsgBox.ShowWarnBox("货代ID号不能为空");
                txtCorpCod.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(ey.CNTR_CORP_NAM.Trim()))
            {
                TT.HelpInfo.MsgBox.ShowWarnBox("货代名称不能为空");
                txtCorpNam.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(ey.ENABLE_FLAG.Trim()))
            {
                TT.HelpInfo.MsgBox.ShowWarnBox("生效状态不能为空");
                cboEnable_Flag.Focus();
                return false;
            }
            return true;
        }
        #endregion

        private void btnSec_ItemClick(object sender, ItemClickEventArgs e)
        {
            TT.HelpInfo.MsgBox.ShowSuccessFailBox(
            global.bzUser.CreateFormSecrity(this, barManager1.Items, global.basSysPara.UserID, this.MOD_ID)
            );
        }
    }
}
